// 购物车状态管理
import { ref, computed } from 'vue'
import { defineStore } from "pinia";
import { useUserInfoStore } from './userInfo';
import { insertCartAPI, getCartListAPI, delCartAPI, mergeCartAPI } from '@/apis/cart'

export const useCartStore = defineStore('cart', () => {
    const userInfoStore = useUserInfoStore()
    const isLogin = computed(() => userInfoStore.userInfo.token)

    // 购物车数据
    const cartList = ref([])

    // 更新数据
    const updateCartList = async () => {
        const res = await getCartListAPI()
        cartList.value = res.result
    }

    // 合并购物车工
    const mergeCart = async () => {
        const query = cartList.value.map(({ skuId, selected, count }) => ({ skuId, selected, count }))
        mergeCartAPI(query)
    }


    // 添加一个商品
    const addCart = async (obj) => {
        const { skuId, count } = obj
        // 如果已经登录
        if (isLogin.value) {
            await insertCartAPI({ skuId, count })
            updateCartList()
        } else {
            // 判断cartList里面是否有这个商品
            const index = cartList.value.findIndex((item) => item.skuId === obj.skuId)
            if (index === -1) {
                cartList.value.push(obj)
            } else {
                cartList.value[index].count += obj.count
            }
        }

    }

    // 删除一个商品
    const delCart = (skuId) => {
        if (isLogin.value) {
            delCartAPI([skuId])
            updateCartList()
        } else {
            const index = cartList.value.findIndex((item) => item.skuId === skuId)
            if (index !== -1) {
                cartList.value.splice(index, 1)
            }
        }

    }

    // 商品总数量
    const allCount = computed(() => {
        return cartList.value.reduce((a, c) => a + c.count, 0)
    })
    // 选中的总数量
    const allCheckCount = computed(() => {
        return cartList.value.reduce((a, c) => {
            if (c.selected) return a + c.count
            return a
        }, 0)
    })

    // 商品总价格
    const allPrice = computed(() => {
        return cartList.value.reduce((a, c) => a + c.count * c.price, 0).toFixed(2)
    })
    // 选中的总价格
    const allCheckPrice = computed(() => {
        return cartList.value.reduce((a, c) => {
            if (c.selected) return a + c.count * c.price
            return a
        }, 0).toFixed(2)
    })

    // 所有项已勾选的值
    const allSelected = computed(() => {
        return cartList.value.every((item) => item.selected === true)
    })

    // 勾选单个
    const check = (val, skuId) => {
        const index = cartList.value.findIndex((item) => item.skuId === skuId)
        cartList.value[index].selected = val
    }
    // 勾选所有方法
    const checkAll = (val) => {
        cartList.value.forEach(item => { item.selected = val })
    }

    // 清楚数据
    const clearAll = () => {
        cartList.value = []
    }

    return { cartList, addCart, delCart, allCount, allPrice, allSelected, checkAll, allCheckCount, allCheckPrice, check, clearAll, updateCartList, mergeCart }

}, { persist: true })